package org.biubiu0002.baiblog.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.biubiu0002.baiblog.common.exception.BaiBlogException;
import org.biubiu0002.baiblog.common.exception.BizCodeEnumException;
import org.biubiu0002.baiblog.common.utils.PageQuery;
import org.biubiu0002.baiblog.common.utils.PageUtils;
import org.biubiu0002.baiblog.common.utils.Query;
import org.biubiu0002.baiblog.core.dao.RichTextDao;
import org.biubiu0002.baiblog.core.model.entity.RichTextEntity;
import org.biubiu0002.baiblog.core.service.RichTextService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("richTextService")
public class RichTextServiceImpl extends ServiceImpl<RichTextDao, RichTextEntity> implements RichTextService {

    @Resource
    private RichTextDao richTextDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<RichTextEntity> page = this.page(
                new Query<RichTextEntity>().getPage(params),
                new QueryWrapper<RichTextEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public Map<String, RichTextEntity> getRichTextEntityMap(List<String> textIdList) {
        if (CollectionUtils.isEmpty(textIdList)) {
            return Collections.emptyMap();
        }
        List<RichTextEntity> list = this.list(new QueryWrapper<RichTextEntity>().in("resource_id", textIdList).eq("is_deleted", 0));
        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyMap();
        }
        return list.stream().collect(Collectors.toMap(RichTextEntity::getResourceId, e -> e, (oldV, newV) -> newV));
    }

    @Override
    public RichTextEntity getByTextId(String textId) {
        return this.getOne(new QueryWrapper<RichTextEntity>()
                .eq("resource_id", textId)
                .eq("is_deleted", 0)
        );

    }

    @Override
    public IPage<RichTextEntity> searchPage(String key, int type, PageQuery<RichTextEntity> pageQuery) {

        List<RichTextEntity> richTextEntities = searchPageList(key, type, pageQuery);
        pageQuery.setRecords(richTextEntities);
        return pageQuery;
    }

    @Override
    public void updateShareTextStatus(List<String> shareIdList, Integer newStatus) {
        if(CollectionUtils.isEmpty(shareIdList)){
            throw new BaiBlogException(BizCodeEnumException.INNER_INPUT_EXCEPTION, "传入集合不能为空");
        }
        richTextDao.updateShareTextDeletedBatch(shareIdList,newStatus);
    }

    @Override
    public void updateCommentTextStatus(List<String> commentIdList, Integer newStatus){
        if(CollectionUtils.isEmpty(commentIdList)){
            throw new BaiBlogException(BizCodeEnumException.INNER_INPUT_EXCEPTION, "传入集合不能为空");
        }
        richTextDao.updateCommentTextDeletedBatch(commentIdList,newStatus);
    }

    public List<RichTextEntity> searchPageList(String key,int type, PageQuery<RichTextEntity> pageQuery){
        return richTextDao.searchPage(key,type,pageQuery);
    }



}